// index.js
// 获取应用实例
// 首页
import api from '../../api/index';
const app = getApp()

Page({
  data: {
    statusBarHeight: '',
    banners: [
      "https://file.xssbdc.com/static/app/banner/banner.png",
      "https://file.xssbdc.com/static/app/banner/banner2.png",
      "https://file.xssbdc.com/static/app/banner/banner3.png"
    ],
    autoplay: true,
    interval: 2000,
    duration: 500,
    currentIndex: 0,
    promotion_center_list: [],
    noReading: 0, //未读数
    myPromotion: {}, //我的推广
    userInfo: {}, //个人信息
    commissionInfo: {}, //佣金信息
    isOpenActivity: false, //是否开启活动
    isShowRedEnvelope: true, //是否显示红包弹层
    noRedEnvelopesOrNotLoggedIn: true, //没用红包或者未登录
    redEnvelopeNum: 0, //红包次数
    isSplit: false, //是否拆包
    amount: 0, //红包金额
    showMes:false,//显示消息推送弹层
    mesNum:0,//新消息条数
  },

  onLoad(options) {
    if (!wx.getStorageSync('privacy')) {
      console.log('未同意')
      //跳转弹窗页
      wx.redirectTo({
        url: '/packageA/pages/privacy-agreement-layer/index'
      })
    } else {
      console.log('同意进入')
      this.setData({
        statusBarHeight: app.globalData.statusBarHeight
      })
      this.getRanking();
      this.getPromotionCenter();
      this.getBanner();
      
    };
   
// console.log(app.globalData.isShowPrompt);
   

  },
  onShow() {
    let token = wx.getStorageSync('token');
    if (token) {
      this.getNotReadNum();
      this.getUserInfo();
      this.getMyPromotion();
      this.queryMessage();
      
    } else {
      this.setData({
        myPromotion: {},
      })
    }
    this.getCommissionPublicity();
    this.getActivity();
    this.getRedEnvelope();

  },
  //关闭消息推送弹层
  close(){
    this.setData({
      showMes:false
    })
  },

  //消息推送查询
  queryMessage(){
    api.queryMessage().then(res=>{

      this.setData({
        mesNum:+res.data.data,
        showMes:res.data.data>0?true:false
      })
    })
  },


  //查看图片
  viewImg() {
    api.getActivityPoster().then(res => {
      wx.previewImage({
        current: res.data.data, // 当前显示图片的http链接
        urls: [res.data.data] // 需要预览的图片http链接列表
      })
    })
  },
  //关闭或者再来一次
  shutDownOrTryAgain() {
    if (this.data.redEnvelopeNum > 0) {
      //在拆一次
      this.openRedEnvelope();
    } else {
      //关闭弹层
      // this.closeLayer();
      //查看海报
      this.viewImg();
    }
  },
  //拆红包
  openRedEnvelope() {
    api.getRedEnvelopesMoney().then(res => {
      this.setData({
        amount: res.data.data,
        isSplit: true,
        redEnvelopeNum: --this.data.redEnvelopeNum
      })
    })
  },
  //获取红包次数
  getRedEnvelope() {
    api.getRedEnvelopesTimes().then(res => {
      this.setData({
        redEnvelopeNum: res.data.data,
        noRedEnvelopesOrNotLoggedIn: res.data.data > 0 ? false : true
      })
    })
  },
  //关闭红包
  closeLayer() {
    this.setData({
      isShowRedEnvelope: false
    })
  },
  //是否开启活动
  getActivity() {
    api.getActivity().then(res => {
      this.setData({
        isOpenActivity: res.data.data.itemValue == '1' ? true : false
      })
    })
  },
  //去banner详情
  goBannerDetail(e) {
    let url = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/bannerDetail/index?url=' + url,
    })
  },
  //获取banner
  getBanner() {
    api.plainDictionary('banner_img').then(res => {
      this.setData({
        banners: res.data.data
      })
    })
  },
  //获取佣金公示
  getCommissionPublicity() {
    api.getCommissionPublicity().then(res => {
      this.setData({
        commissionInfo: res.data.data
      })

    })
  },
  //获取个人信息
  getUserInfo() {
    api.getUserInfo().then(res => {
      this.setData({
        userInfo: res.data.data
      })
    })
  },
  //我的推广
  getMyPromotion() {
    api.getMyPromotion().then(res => {
      this.setData({
        myPromotion: res.data.data
      })
    })
  },
  //扫码
  scanCode() {
    wx.scanCode({
      success(res) {
        if (res.path) {
          //小程序码
          wx.navigateTo({
            url: '/' + res.path
          })
        }
        console.log('扫码', res)
      }
    })
  },
  //获取未读数
  getNotReadNum() {
    api.getNotReadNum().then(res => {
      this.setData({
        noReading: res.data.data
      })
    })
  },
  //推广
  promote(e) {
    let id = e.currentTarget.dataset.id
    if (id == 5) {
      //房源推广
      wx.navigateTo({
        url: `/pages/housing-resources/list/index?id=${id}`,
      })
    } else if (id == 6) {
      //新手任务
      wx.navigateTo({
        url: `/pages/new-user-registration/index?id=${id}`,
      })
    } else if (id == 7) {
      //推广海报赚钱
      wx.navigateTo({
        url: `/pages/promote-posters-make-money/index?id=${id}`,
      })
    } else if (id == 4) {
      wx.showToast({
        icon: 'none',
        title: '敬请期待',
      })
    } else {
      //产品列表
      wx.navigateTo({
        url: `/pages/real-estate-mortgage/index?id=${id}`,
      })
    }
  },
  //  导航跳转
  jump(e) {
    let url = e.currentTarget.dataset.url;
    let type = e.currentTarget.dataset.type;
    if (type == 'promote') {
      wx.navigateTo({
        url: url,
      })
    } else {
      wx.switchTab({
        url: url,
      })
    }

  },
  // 推广中心
  getPromotionCenter() {
    api.getExtension().then(res => {
      this.setData({
        promotion_center_list: res.data.data
      })
    })

  },
  //获取个人业绩和排名
  getRanking() {
    api.getExtIncome().then(res => {
      console.log(res)
    })
  },
  //banner改变
  onSwiperChange(e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },



})